Programming Concepts


Objectives : Student should be able to -


The concept of a Program :

A program is a sequence of instructions or programming language statements written to make a computer perform certain tasks.

Well-structured programs require a programming language to support the following program constructs :

A computer's processor can only run a computer program in the form of a file of machine code, which is a sequence of binary codes representing instructions for the processor.

When machine code runs, the processor repeatedly :



Q1. a)  Describe  Variable  and  Constant  with example used in a program.

✬   Variable 

Variable is a named location in computer memory that stores data, its value can change during the execution of the program.

It is important to give a sensible names to a variable for easy understanding and maintainability of the program.

Example :   Amount ‹— 5 * Quantity
where 'Amount' and 'Quantity' are variables.

✬   Constant 

Constant is a named location in computer memory that stores data, its value do not change during the execution of the program.

It is important to give a sensible names to a constant for easy understanding and maintainability of the program.

Example :   CONSTANT   Pie ‹— 3.142
where 'Pie' is a constant whose value is fixed and do not change during the execution of the program.

b)  Describe how a variable is declared in a program.

⇒  Variable is declared or defined with key-word DECLARE followed by the name of variable (identifier), a colon (:) symbol followed by its data-type it could hold (like, Text/String, Integer, Real, ect.).

⇒  Variables have to be declared before they are used in a program.

Example :  DECLARE Mark : INTEGER

where 'Mark' is the name of the variable and INTEGER is its data-type.

It is optional to assign an initial value to a variable during declaration (called Initialization).
Variable is assigned a value '0' at start of the program, when it is used for 'Counting', or to store the 'Running Total'.

c)  What are the rules of naming a variable or constant.

⇒  Variable names should not have space in them (use underscores instead).

⇒  Variable names should not start with a digit.

⇒  Reserved words cannot be used for naming a variable, like INPUT, PRINT, LET, SET, etc. and name of the Sub-routines or Library routines.

d)  Describe the following  Data types  assigned to a variable with example.

 Integer  : An Integer is a positive or negative whole number that can be used with mathematical operators.

Example :  12,   3,   -5,   10,   -9 etc.

 Real (or) Float  : A Real number is a positive or negative number with a fractional part that can be used with mathematical operators.

Example :  3.142,   -7.25,   4.5,   -24.63,   0 etc.

 String (or) Text  : A String is a data type assigned to a variable or constant that can hold a combination of any characters, digits, or symbols of any length.

Example :  John Fernandaz,   john1998@gmail.com,   MV5228907, etc.

 Char (or) Character  : A Char is a data type assigned to a variable or constant that can hold only one character.

Example :  F,   M,   $,   N,   @, . . . etc.

 Boolean  : A Boolean is a data type assigned to a variable or constant that can hold only two possible values, like, either True or False.

Example :  True or False,   Yes or No.

Q2. a)  What is meant by variable initialization and why is it important ?

⇒  Initializing a variable means asigning a correct initial value to the variable before it's use.

⇒  For totalling or counting process, the variable should be initialised with value 0, because totalling and counting of an item starts with value 0.

b)  Why it is a good practice to define a constant ?

⇒  Constants enable you to use the same name to identify the same value throughout your code.

⇒  If you need to update the constant's value, then you don't have to change every instance of the value.

⇒  You just have to change the value in a single place. This improves your code's maintainability.

Q3.  List the Arithmetic Operators used to define an expression within the program.

Arithmetic operation Operator
Addition +
Subtraction -
Multiplication *
Division /

Q4.  List the Relational Operators used in computer programming.

Meaning for Relational operator Operator
Assignment operator
(used to assign a value to a variable)
Equal to =
More than >
Less than <
More than or equal to > =
Less than or equal to < =
Not equal to < >

Q5.  List the Logical Operators used in computer programming.

Meaning for Logical operator Operator
Both side must be true AND
One side or other must be true OR
One side or other must be true but not both XOR
Negates truth NOT

Q6.  What is procedural programming?.

⇒  Procedural Programming is the use of Top-down approach, breaking down programming task into a collection of variables, data structures, and subroutines.

⇒  The instructions are executed in the order defined by the programmer.

⇒  The steps are normally "sequence," "selection, " "iteration," and a “case-type” statement.

⇒  The algorithm stops after execution of finite number of instructions.

Q7.  Describe the following three basic building blocks or programming constructs.

  1. Sequential statement.
  2. ☛  Sequential statement is an instruction to a computer in one line to do a task.

    ☛  Sequential statements are a set of instructions that are executed in a sequential order (from top to bottom) one after another in which they appear within a program to solve a problem.

    Example of sequential statements are :

    Declaring variables : When devising an algorithm, the programmer require a variable to be defined and given a data type before use. Like -

    DECLARE X : INTEGER

    Where, X is a variable of data type Integer.


    Declaring constants : When devising an algorithm, the programmer may require a named data store called constant to hold a value that does not change during the execution of a program. Like -

    CONSTANT  PI 3.142

    Where, PI is a constant that holds a fixed value 3.142


    Initialization of variable : Assigning a value to a variable during declaration before using it is called Initialization.
    It is indicated by using an arrow symbol (). The arrow points the value being assigned towards the variable. Like -

    X 0
    Minimum 1000
    Maximum 0


    Assignment statement : Assigning a formula to a variable to store its value is indicated an arrow symbol () pointing towards the variable. Like -

    Count Count + 1
    Sum Sum + Mark
    Avg Sum/Count


    OUTPUT statement : Output statement returns and displays some data to the user.
    This could be in the form of a written message, like -

    OUTPUT "Hello World."

    Sometimes the word "PRINT" may be used instead of OUTPUT, as in the following example -

    PRINT "Hello World."

    Following statement, returns the value of the average marks stored in the variable "Avg".

    OUTPUT "Average marks of the students is ",  Avg


    INPUT statement : Input statement is used when we expect some value to be provided by the user.
    The following example prompts the user with output of a message asking to input their name and assign it to a variable called 'name'.

    PRINT "What is your name?"
    INPUT name
                            /Or/
    INPUT "What is your name?", name

    Sometimes the word "READ" may be used instead of INPUT, like-

    READ "Enter the mark of the student :"
    READ  mark

  3. Selection / Conditional statement.
  4. ☛  Selection statement allows a program to test the given conditions and chose the instructions to be executed if it is true.

    There are two types of Selection structures (compound statements) :

    1. IF.. THEN.. ELSE.. ENDIF
    2. IF (condition = true) THEN (Instruction-A) ELSE (Instruction-B) ENDIF - gives two possible choices (paths) that a program can follow. IF condition is "true" then Instruction-A will be executed, if it is "false" then Instruction-B" will be executed.

      However, sometimes more than two choices are wanted. To do this, the statement ELSE IF is used.

      The condition used is based on "Relational operator", (like >, <, =, NOT, AND, OR).

      Example :  INPUT Length
      IF Length > 0 THEN
      Area = Length * Length
      OUTPUT Area
      ELSE
      OUTPUT "Error, length is negative or 0."
      ENDIF
    3. CASE.. OF.. OTHERWISE.. ENDCASE
    4. CASE.. OF.. OTHERWISE.. ENDCASE - gives choice between several different values, whose condition is based on "Equality operator".

      It is a switching statement, whose control jumps to the corresponding case, as per the value of the switch (variable).

      The condition used is based on "Equality operator".

      Example :  INPUT Color
      CASE Color OF
      "green" : OUTPUT "Grass"
      "yellow" : OUTPUT "Sun"
      "blue" : OUTPUT "Sky"
      OTHERWISE
      OUTPUT "Don't know that color"
      ENDCASE
  5. Iteration / Looping structure.
  6. ☛  Iteration is the term given to the repetition of a block of instructions (code) within a computer program for a given number of repetitions or continuously either for as long as a condition continues to be true or until a condition becomes true.

    ☛  When a block of code is executed out again, it is called an iteration.
    When a cycle of instructions is carried out in a repeated manner, it is called a loop.

    ☛  Iteration allows programmers to simplify a program and make it more efficient. Instead of writing out the same lines of code again and again, a programmer can write a section of code once, and ask the program to execute the same block of code repeatedly until it is no longer needed.

    There are three types of Looping structures :

    1. FOR … TO … NEXT   is called Counting Loop.
    2. The FOR-NEXT loop is used to repeat code for a given number of repetitions. We specify a counter variable and set it to an initial value and then specify an end value. After every iteration of the loop the counter variable is automatically incremented by 1.

      In the following example, the counter variable "X" starts at 1 and increases by 1 every time the code repeats until it reaches a value of 10, then the loop will terminate.

      Example :  FOR X = 1 TO 10
      INPUT Num
      Total = Total + Num
      NEXT X
    3. WHILE.. DO.. ENDWHILE   is a Conditional Loop.
    4. The WHILE.. DO.. ENDWHILE loop, executes the block of code and continue to loop or repeat only if the condition is True.

      The WHILE.. DO.. ENDWHILE loop, checks for the condition before executing the block of code.

      The WHILE.. DO.. ENDWHILE loop, may not execute the block of code if the condition is False.

      The following example will continue to take user input till the value of Count is less than 10 (ten times : for Count = 0 to 9).

      Example :  Count 0
      WHILE Count < 10 DO
      INPUT Num
      Count Count + 1
      ENDWHILE
      flowchart_While_do_loop
    5. REPEAT.. UNTIL   is a Conditional Loop.
    6. The REPEAT.. UNTIL loop, executes the block of code and continue to repeat until the condition is True (repeats only if the condition is False).

      The REPEAT.. UNTIL loop, checks for the condition after executing the block of code.

      The REPEAT.. UNTIL loop, need to be executed at least once.

      The following example will continue to take user input until the user inputs a value between 0 and 50 inclusive.
      This REPEAT.. UNTIL loop structure is often used for Validation checks.

      Example :  REPEAT
      INPUT Num
      IF (Num < 0 OR Num > 50) THEN
      PRINT "Invalid data, please re-enter the number."
      ENDIF
      UNTIL (Num >= 0 AND Num <= 50)
      flowchart_Repeat_Until_loop

Q8.  Describe the possible three different ways, the conditional  "IF" statement  could be used.

  1. IF statements could be used without ELSE clause as follows :
  2. IF statements with an ELSE clause is written as follows :
  3. IF (Mark >= 60)
    THEN
    PRINT "Passed"
    ELSE
    PRINT "Failed"
    ENDIF

    IF (<condition>) is "true" (THEN instructions) will be executed, if it is "false" (ELSE instructions) will be executed.

  4. Nested IF statements : Allows "IF statement" inside another "IF statements", which could be written as follows :
  5. IF (Mark >= 90)
    THEN
    PRINT "You earned an A-grade."
    ELSE
    IF (Mark >= 80)
    THEN
    PRINT "You earned a B-grade."
    ELSE
    PRINT "You earned a C-grade."
    ENDIF
    ENDIF

Q9.  Give three difference between  “IF … ELSE … ENDIF”  and  “CASE … OF … OTHERWISE … ENDCASE”  conditional selection statements.

  1. If-Else-EndIf statements works on the basis of relational operator to check if the condition is true.
    Case-Otherwise-EndCase statement work on the basis of equality operator to check if the condition is true.
  2. If-Else-EndIf control goes through the every else-if statement until it finds true value of the statement in nested IF statement.
    Case-Otherwise-EndCase control jumps to the corresponding case, as per the value of the switch.
  3. Case-Otherwise-EndCase switching statement is faster in execution than If-Else-EndIf conditional statement as it tests only a single expression against a list of discrete values.

Q10.  Give three difference between  “WHILE … DO … ENDWHILE”  and  “REPEAT … UNTIL”  conditional loop structures.

  1. WHILE … DO … ENDWHILE checks for the condition before executing the looping statements.
    REPEAT … UNTIL checks for the condition after executing the statements once.
  2. WHILE … DO … ENDWHILE loop executes only if the condition is true.
    REPEAT … UNTIL loop executes only if the condition is false.
  3. WHILE … DO … ENDWHILE looping statements may not be executed at all.
    REPEAT … UNTIL looping statements need to be executed at least once.

Q11.  A times table test will be set up. Before using the test, the user can set the times table to be tested and the number of questions to be asked. The times tables can be any whole number between 2 and 12 inclusive. The number of questions can be between 5 and 10 inclusive. No questions can be the same in any test. The name of the user should be displayed with all messages and prompts.

Write and test a program that meets the following requirements :

You must use pseudocode or program code and add comments to explain how your code works. All inputs and outputs must contain suitable messages.

'// Declaring the variables
DECLARE  Name : STRING
DECLARE  Table, Questions, Qs, RightAns, Answer : INTEGER
DECLARE  ReTest : BOOLEAN

OUTPUT  “Enter your name : ”
INPUT  Name

'// Ask to input the times table with validation between 2 and 12 inclusive
REPEAT
OUTPUT  “Please enter a times table between 2 and 12 :”
INPUT  Table
IF  Table < 2 OR Table > 12  THEN
OUTPUT  “Invalid table”
ENDIF
UNTIL  Table >= 2 AND Table <= 12

'// Ask to input the number of questions with validation between 5 and 10 inclusive
REPEAT
OUTPUT  “Please enter the number of questions between 5 and 10 :”
INPUT  Questions
IF  Questions < 5 OR Questions > 10  THEN
OUTPUT  “Invalid number of questions”
ENDIF
UNTIL  Questions >= 5 AND Questions <= 10

‘// Initialize a Tag-variable for re-test with value “Yes”
ReTest ‹— Yes

‘// Conduct the Test
WHILE  ReTest = Yes  DO
‘// Initialize a variable to count right answers
RightAns ‹— 0
‘// Ask questions for times table
FOR  Qs = 1 TO Questions
OUTPUT  “Question-”, Qs
OUTPUT  Table, “ x ”, Qs, “ = ”
INPUT  Answer
IF  Answer = Table * Qs  THEN
OUTPUT  Name, “ your answer is correct.”
RightAns = RightAns + 1
ELSE
OUTPUT  Name, “ your answer is wrong.”
OUTPUT  “The correct answer is ”, Table * Qs
ENDIF
NEXT  Qs

OUTPUT  “Test over - ”, Name, “ you got ”, RightAns, “ out of “, Questions

‘// Ask their choice for re-test
OUTPUT  “Do you want a re-test (Yes or No) : ”
INPUT  ReTest

ENDWHILE



* * * * * * * * *
* * * * * *
* * *
*